﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using Newtonsoft.Json;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element
{
    [JsonObject(MemberSerialization.OptIn)]
    public sealed class Bezier : BasicElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("点 1"), JsonProperty]
        public required Point P1 { get; set; }

        [Category("基本"), DisplayName("点 2"), JsonProperty]
        public required Point P2 { get; set; }

        [Category("基本"), DisplayName("点 3"), JsonProperty]
        public required Point P3 { get; set; }

        [Category("基本"), DisplayName("点 4"), JsonProperty]
        public required Point P4 { get; set; }

        public Bezier()
        {
            this.Draw += Bezier_Draw;
            this.Load += Bezier_Load;
        }

        private void Bezier_Draw(object sender, DrawEventArgs e)
        {
            e.Graphics.DrawBezier(Utilities.ColoredPen(Pen.Create(), e.Color), P1, P2, P3, P4);
        }

        private void Bezier_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = data;
        }

        public override string ToString() => "贝塞尔曲线";
    }

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class BezierBatch : BasicElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("关键点"), JsonProperty]
        public required PointF[] Points { get; set; }
        public BezierBatch()
        {
            this.Draw += BezierBatch_Draw;
            this.Load += BezierBatch_Load;
        }
        private void BezierBatch_Draw(object sender, DrawEventArgs e)
        {
            e.Graphics.DrawBeziers(Utilities.ColoredPen(Pen.Create(), e.Color), [..Points]);
        }

        private void BezierBatch_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = Data;
        }

        public override string ToString() => "贝塞尔曲线组";
    }
}
